package com.xizi.Transactional;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


public class AccountService2222 implements AccountService22 {

    private JdbcTemplate jdbcTemplate;

    private static String insert_sql = "insert into account(balance) values (100)";


    @Override
    public void save() throws RuntimeException {
        System.out.println("==开始执行sql");
        jdbcTemplate.update(insert_sql);
        System.out.println("==结束执行sql");

        System.out.println("==准备抛出异常");
        throw new RuntimeException("==手动抛出一个异常");
    }

    //分析Spring事物的实现过程了，因为这里会涉及到事物传播特性、事物嵌套调用等等
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class,)
    public void delete() throws RuntimeException {
        System.out.println("==调用AccountService的delete方法\n");
        jdbcTemplate.update(insert_sql);

        throw new RuntimeException("==AccountService的delete方法手动抛出异常");
    }


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
